package com.own.business.system.dictionary.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.own.business.system.dictionary.business.service.SystemDictionaryService;
import com.own.business.system.dictionary.common.em.DictionaryExceptionEnum;
import com.own.business.system.dictionary.common.exception.DictionaryException;
import com.own.business.system.dictionary.entity.bo.SystemDictionaryBo;
import com.own.business.system.dictionary.entity.map.SystemDictionaryMapVo;
import com.own.business.system.dictionary.entity.po.SystemDictionary;
import com.own.business.system.dictionary.entity.query.SystemDictionaryQuery;
import com.own.business.system.dictionary.entity.vo.SystemDictionaryVo;
import com.own.business.system.dictionary.mapper.SystemDictionaryMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.component.common.model.UpdateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * SystemDictionaryServiceImpl
 *
 * @author chenxueli
 * @date 2023-06-21 09:54:27
 */
@Slf4j
@Service
public class SystemDictionaryServiceImpl extends AbstractBaseService<
        SystemDictionary,
        SystemDictionaryBo,
        SystemDictionaryVo,
        SystemDictionaryMapVo,
        SystemDictionaryQuery,
        SystemDictionaryMapper
        > implements SystemDictionaryService {

    @Resource
    private SystemDictionaryMapper systemDictionaryMapper;

    @Override
    public LambdaQueryWrapper<SystemDictionary> lambdaQueryWrapper(SystemDictionaryQuery query) {
        return super.lambdaQueryWrapper(query)
                .like(StringUtils.isNotEmpty(query.getName()), SystemDictionary::getName, query.getName())
                .eq(StringUtils.isNotEmpty(query.getCode()), SystemDictionary::getCode, query.getCode())
                .orderByAsc(SystemDictionary::getSortOrder)
                .orderByDesc(SystemDictionary::getGmtCreate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<SystemDictionaryVo> add(SystemDictionary systemDictionary) {
        // 判断是否已经重名
        var count = countEqual(SystemDictionary::getCode, systemDictionary.getCode());
        if (count > 0) {
            throw new DictionaryException(DictionaryExceptionEnum.CODE_EXIST);
        }
        return super.add(systemDictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<SystemDictionaryVo> update(SystemDictionary systemDictionary) {
        var wrapper = lambdaQueryWrapper()
                .eq(SystemDictionary::getCode, systemDictionary.getCode())
                .ne(SystemDictionary::getId, systemDictionary.getId());
        var count = systemDictionaryMapper.selectCount(wrapper);
        if (count > 0) {
            throw new DictionaryException(DictionaryExceptionEnum.CODE_EXIST);
        }
        return super.update(systemDictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<Boolean> delete(Long pk) {
        return new UpdateModel<>().check(
                super.delete(pk)
        );
    }

    /**
     * 根据code获取数据字典信息
     *
     * @param code code信息
     * @return 数据字典信息
     */
    @Override
    public SystemDictionaryVo getByCode(String code) {
        var wrapper = lambdaQueryWrapper().eq(SystemDictionary::getCode, code);
        var po = getOneByWrapper(wrapper);
        if (po != null) {
            return new SystemDictionaryVo(po);
        }
        return null;
    }
}
